<% content_for :admin_tools do %>
  <% if @tag && authorized?(@tag) %>
    <p class="download-link download-csv">
      <%= link_to "Download Attendance History", tag_path( @tag, :format => :csv ) %>
    </p>
  <% end %>
<% end %>

<% cache(["#{date_slug}/tags/show/upcoming", @tag], :tag => 'session-info', expires_in: 1.day)  do %>
  <% @all_topics = Topic.tagged_with(@tag).active %>
  <% if @all_topics.size > 0 %>
    <div class="content-section">
      <h2>Upcoming sessions
        <%= link_to( 
          "Subscribe to the XML feed for this tag",
           tag_path(@tag, :format => :atom),
           :class => 'feed-link') %>
      </h2>
      <% @topics = Topic.upcoming.tagged_with(@tag).includes(:sessions) %>
      <% if @topics.present? %>
        <ul class="topic-list">
          <% @topics.sort_by(&:next_time).each do |topic| %>
            <% cache(["#{date_slug}/tags/show/upcoming", topic], :tag => topic.cache_key, expires_in: 1.day) do %>
              <li>
                <%= link_to topic.name, topic %>
                <% topic.upcoming_sessions.first(2).each do |session| %>
                  <div class="session-info">
                    <span class="session-link"><%= link_to_session session %></span>
                    <%= session_site(session) %>
                  </div>
                <% end %>
                <% more = topic.upcoming_sessions.size - 2 %>
                <% if more > 0 %>
                  <div class="session-info">
                    <span class="session-link"><%= link_to "#{more} more upcoming sessions", topic %></span>
                  </div>
                <% end %>
              </li>
            <% end %>
          <% end %>
        </ul>
      <% else %>
        <div>This are currently no upcoming sessions for topics tagged with '<%= @tag.name %>'.</div>
      <% end %>
    </div>

    <div class="content-section">
      <h2>All topics offered tagged with '<%= @tag.name %>'</h2>
      <ul class="topic-list">
        <% @all_topics.sort_by {|a| a.name.downcase }.each do |topic| %>
          <% cache(["tags/show/all", topic], :tag => topic.cache_key) do %>
            <li>
              <%= link_to topic.name, topic %>
            </li>
          <% end %>
        <% end %>
      </ul>
    </div>
  <% else %>
    <div class="empty-list">This are currently no topics tagged with '<%= @tag.name %>'.</div>
  <% end %>
<% end %>
